package com.example.spider.provider.why10w.deprecated.wgwhy10.handler;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import com.example.spider.provider.why10w.deprecated.wgwhy10.Wgwhy10ListTaskFactory;
import com.example.spider.provider.why10w.deprecated.wgwhy10.task.Wgwhy10DetailTask;
import com.example.spider.provider.why10w.deprecated.wgwhy10.task.Wgwhy10ListTask;
import com.example.spider.task.SpiderResultHandler;
import com.example.spider.task.SpiderTaskQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 处理列表查询结果
 *
 * @author lym
 */
@Slf4j
//@Component
public class Wgwhy10ListResultHandler implements SpiderResultHandler<Wgwhy10ListTask, String> {


    /**
     * 分析列表结果，创建下载详情任务
     */
    @Override
    public void handle(Wgwhy10ListTask task, String result) {

        log.debug("prepare handing Wgwhy10List " + task.getUrl());

        String hasContentRegex = "<div class=\"mainl\">(.*?)<!--@ mainl-->";
        List<String> contentStr = ReUtil.findAllGroup1(hasContentRegex, result);
        if (contentStr.isEmpty()) {
            // 爬完了都，会返回404页面，是没有内容的
            log.error("not a valid result. May finished!");
            return;
        }
        String content = contentStr.get(0);
        String urlAndTitleRegex = "<h2><a href=\"(.*?)\" target=\"_blank\">(.*?)</a></h2>";
        Pattern urlAndTitlePattern = PatternPool.get(urlAndTitleRegex, 32);

        // 一页大概10项
        List<Wgwhy10DetailTask> detailTaskList = new ArrayList<>(15);
        ReUtil.findAll(urlAndTitlePattern, content, m -> {
            Wgwhy10DetailTask detailTask = new Wgwhy10DetailTask()
                    .setTitle(m.group(2))
                    .setCategory(task.getCategory())
                    .setUrl(m.group(1));
            detailTaskList.add(detailTask);
        });


        if (CollectionUtils.isEmpty(detailTaskList)) {
            log.warn("items is empty");
            return;
        }
        // 创建爬取详情的任务
        detailTaskList.forEach(SpiderTaskQueue::putTask);

        // 创建爬取下一页的任务
        String nextPageMark = ">››</a>";
        if(result.contains(nextPageMark)) {
            Wgwhy10ListTask nextPageTask = Wgwhy10ListTaskFactory.createPageTask(task.getCategory(), task.getPage() + 1);
            SpiderTaskQueue.putTask(nextPageTask);
        }
        log.info("finished handle {} {} ", task.getClass().getSimpleName(), task.getUrl());
    }

}
